package com.barneyx.mshinfoplatform.web.admin.admin;

import com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.barneyx.mshinfoplatform.common.ExResult;
import com.barneyx.mshinfoplatform.entity.admin.AdminUserEntity;
import com.barneyx.mshinfoplatform.exception.AdminUserException;
import com.barneyx.mshinfoplatform.service.admin.IAdminUserEntityService;
import com.barneyx.mshinfoplatform.util.admin.AdminUtil;
import lombok.extern.slf4j.Slf4j;
import org.springframework.web.bind.annotation.*;

import javax.annotation.Resource;
import java.util.List;

@RestController
@RequestMapping("/api/admin/account/user/")
@Slf4j
public class AdminUserController {
    @Resource
    private IAdminUserEntityService adminUserService;

    @Resource
    private AdminUtil adminUtil;


    @GetMapping("user-page")
    public ExResult<?> getPage(@RequestParam(value = "current", defaultValue = "1", required = false) long current,
                               @RequestParam(value = "size", defaultValue = "20", required = false) long size) {
        Page<AdminUserEntity> page = adminUtil.adminUserService.page(new Page<>(current, size), new LambdaQueryWrapper<AdminUserEntity>().orderByDesc(AdminUserEntity::getCreateAt));
        return ExResult.ofAuto(page);
    }

    @GetMapping("user-list")
    public ExResult<?> getList(@RequestParam(value = "full", required = false) boolean Isfull) {
        if (Isfull) {
            List<AdminUserEntity> list = adminUserService.list(new LambdaQueryWrapper<AdminUserEntity>().orderByDesc(AdminUserEntity::getCreateAt));
            list.forEach(a -> {
                a.setRoles(adminUtil.getAdminUserRolesByAdminUserId(a.getAdminId()));
                a.setControllerAreas(adminUtil.getAreaAdminListByAdminUserId(a.getAdminId()));
            });
            return ExResult.ofAuto(list != null, list);
        } else {
            List<AdminUserEntity> list = adminUserService.list(new LambdaQueryWrapper<AdminUserEntity>().orderByDesc(AdminUserEntity::getCreateAt));
            return ExResult.ofAuto(list != null, list);
        }

    }

    @GetMapping("login")
    public ExResult<?> userLogin(@RequestParam("username") String userName,
                                 @RequestParam("userpwd") String userPwd,
                                 @RequestParam("device") String LoginDevice
    ) throws AdminUserException {
        AdminUserEntity login = adminUtil.login(userName, userPwd, LoginDevice, true);
        login.clearEntityInfoList();
        return ExResult.ofAuto(login);
    }


    @PostMapping("create-user")
    public ExResult<?> createUser(@RequestBody AdminUserEntity adminUser) throws AdminUserException {
        return ExResult.ofAuto(adminUtil.createAdminUser(adminUser));
    }


    @PutMapping("update-user")
    public ExResult<?> updateUser(@RequestBody AdminUserEntity adminUser) throws AdminUserException {
        return ExResult.ofAuto(adminUtil.updateAdminUserInfo(adminUser));
    }

    @DeleteMapping("delete-user-byid")
    public ExResult<?> deleteUserByUserId(@RequestParam("userid") String userId) throws AdminUserException {
        return ExResult.ofAuto(adminUtil.deleteAdminUserByUserId(userId), "删除操作已执行，请自行刷新查看操作结果！");
    }


}
